/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-20
 * Time: 04:01
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;

using Labb4.Pooman.Game;
using Labb4.Pooman.Cards;
using Labb4.Rules;

namespace Labb4.Pooman.Players
{
	/// <summary>
	/// Base class for the different states of the ai players
	/// </summary>
	public abstract class AiGameState
	{
		#region members
		
		/// <summary>
		/// Reference to the table
		/// This is a member only to optimize
		/// </summary>
		protected Table		m_GameTable;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, initializes members
		/// </summary>
		public AiGameState()
		{
			m_GameTable = Table.Instance;
		}
		
		#endregion
		
		#region virtuals
		
		/// <summary>
		/// This method should evaluate the cards won during phase 1
		/// </summary>
		/// <param name="player">The player who took the cards</param>
		/// <param name="cards">The cards taken</param>
		/// <returns>An evaluation enum stating the quality of the won cards</returns>
		public virtual Player.Evaluation EvaluateWonCardsPhase1(Player player, CardPile cards)
		{
			return Player.Evaluation.NA;
		}
		
		#endregion
		
		#region abstracts
		
		/// <summary>
		/// This is where the ai selects cards to play
		/// Must be implemented according to the current phase
		/// </summary>
		/// <param name="player"></param>
		public abstract void SelectCardsAccordingToPhase(Player player);
		
		#endregion
		
		#region protected methods
		
		/// <summary>
		/// Finds the lowest card in a CardPile
		/// </summary>
		/// <param name="cards">The CardPile to look in</param>
		/// <returns>The lowest Card (by Rank) in the given CardPile</returns>
		protected Card LowestCardInPile(CardPile cards)
		{
			Card lowest = cards[0];
			
			foreach(Card it in cards)
			{
				if(it.ValueOfCard < lowest.ValueOfCard)
				{
					lowest = it;
				}
			}
			
			return lowest;
		}
		
		/// <summary>
		/// Finds the highest card value in a pile, not taking the trump into concideration
		/// </summary>
		/// <param name="cards">The cards to evaluate</param>
		/// <returns>The value of the highest card</returns>
		protected int HighestCardvalueInPile(CardPile cards)
		{
			int highest = 0;
			
			foreach(Card it in cards)
			{
				if(it.ValueOfCard > highest)
				{
					highest = it.ValueOfCard;
				}
			}
			
			return highest;
		}
		
		/// <summary>
		/// Finds the highest card value currently on the table, not taking the trump into concideration
		/// </summary>
		/// <returns>The value of the highest card currently on the table</returns>
		protected int HighestCardvalueOnTable()
		{
			int highestCardOnTable = 0;
			
			foreach(CardPile itPile in m_GameTable.CardsOnTable)
			{
				foreach(Card itCard in itPile)
				{
					if(itCard.ValueOfCard > highestCardOnTable)
					{
						highestCardOnTable = itCard.ValueOfCard;
					}
				}
			}
			
			return highestCardOnTable;
		}
		
		#endregion
	}
}
